<template>
	<view>
		<view class="uni-page-head">
			<view class="uni-page-head-title">
				<text class="title_color">{{proInfo.name}}</text>
			</view>
		</view>
		<view class="uni-padding-mt uni-common-mt">
			<form @submit="formSubmit" @reset="formReset">
				<view class="uni-form-item uni-column">
					<view class="uni-title">
						账号（官网默认的登录方式）
						<text>\n</text>
						<text>华医网：账号名，不支持手机与身份证）</text>
					</view>
					<input class="uni-input minput" name="account" placeholder="请输入平台账号,例如:1907280892" />
					<input style="display: none;" type="hidden" name="course_id" :value="proInfo.id">
				</view>
				
				<view class="uni-form-item uni-column">
					<view class="title">密码</view>
					<input class="uni-input minput" name="passwd" placeholder="请输入密码" />
				</view>
				<view class="uni-btn-v">
					<button form-type="submit">提交</button>
				</view>
				<view class="text-box" scroll-y="true">
					<text>如果对你有用，欢迎打赏!</text>
				</view>
				<!-- <view class="uni-center" style="background:#FFFFFF; font-size:0;">
					<image class="image" mode="widthFix" src="../../static/user/my_ali_donte.png" />
					<image class="image" mode="widthFix" src="../../static/user/my_wx_donte.jpg" />
				</view> -->
				
			</form>
		</view>
	</view>
</template>
<script>
	import {mapState, mapMutations} from 'vuex';
	import request_async from '../../common/request_async.js';
	export default {
		data() {
			return {
				proInfo: {
					"id":2,
					"name":"广东省干部学院",
				}
			}
		},
		computed: {
			...mapState(['hasLogin','userInfo'])
		},
		onLoad(option){
			if(option.data == undefined){
				uni.navigateTo({
					url: `/pages/course/index`
				})
				return false;
			}
			
			if (!this.hasLogin) {
				//没有登录
				uni.navigateTo({
					url: "/pages/course/login?data=" + option.data
				})
				return false;
			}
			console.log('option', option);
			this.proInfo = JSON.parse(option.data);
			if(!this.proInfo.id){
				return false;
			}
			if(this.proInfo.id === undefined){
				uni.navigateTo({
					url: `/pages/course/index`
				})
				return false;
			}
		},
		
		methods: {
			async formSubmit(e) {
				var formData = e.detail.value;
				console.log(formData);
				console.log('hasLogin: ', this.hasLogin);
				console.log('form pro: ', this.proInfo);
				console.log('userInfo： ', this.userInfo);
				formData.phone = this.userInfo.phone;
				if(!formData.account){
					this.$api.msg('请输入账号名');
					return;
				}
				if(!formData.passwd){
					this.$api.msg('请输入账号名密码');
					return;
				}
				if(formData.phone && !/(^1[3|4|5|7|8][0-9]{9}$)/.test(formData.phone)){
					this.$api.msg('请输入正确的手机号');
					return;
				}
				uni.showLoading({
					title: '检测账号密码中, 请耐心等待...'
				});
				var res = await request_async({
					name: 'courseuser.add',
					method: 'POST',
					data: formData
				})
				uni.hideLoading();
				console.log('courseuse.add res: ', res);
				if(res.code == 0){
					this.$api.msg('报名成功');
					uni.navigateTo({
						// url: "/pages/course/study?id=" + res.data.id
						url: "/pages/course/detail?id=" + res.data.course_id
					})
				}else{
					this.$api.msg(res.msg);
				}
			},
			formReset: function(e) {
				console.log('清空数据')
			}
		}
	}
</script>

<style>
	.uni-form-item .title {
		padding: 20rpx 0;
	}
	.minput {
		background-color: #efeff4;
		height: 50px;
		line-height:50px;
	}
	.uni-padding-mt {
		padding: 0 15px;
	}
	.title_color {
		color: #007AFF;
	}
	.image {
		margin:40rpx 0;
		width: 300rpx;
	}
</style>
